package com.neusoft.neuiotms.dcom.converter.malfunction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.neuiotms.dcom.dto.malfunction.MalfunctionFrontlineProcessingFormDTO;
import com.neusoft.neuiotms.dcom.model.malfunction.MalfunctionFrontlineProcessingForm;
import com.neusoft.neuiotms.dcom.repository.malfunction.MalfunctionFrontlineProcessingFormRepository;
import com.neusoft.neuiotms.micro.dcam.DcamMicroService;

@Component
public class MalfunctionFrontlineProcessingFormConverter
        extends AbstractConverter<MalfunctionFrontlineProcessingForm, MalfunctionFrontlineProcessingFormDTO> {

    @Autowired
    private MalfunctionFrontlineProcessingFormRepository malfunctionFrontlineProcessingFormRepository;
    @Autowired
    private DcamMicroService assetMicroService;

    @Override
    public MalfunctionFrontlineProcessingForm toModel(final MalfunctionFrontlineProcessingFormDTO dto) {
        MalfunctionFrontlineProcessingForm model;
        if (dto.isNew()) {
            model = new MalfunctionFrontlineProcessingForm();
        } else {
            model = this.malfunctionFrontlineProcessingFormRepository.getOne(dto.getId());
        }
        model.setTaskId(dto.getTaskId());
        model.setWorkNum(dto.getWorkNum());
        model.setFailureLevelCode(dto.getFailureLevelCode());
        model.setFailureLevelName(dto.getFailureLevelName());
        model.setSolved(dto.getSolved());
        model.setWorktypeCode(dto.getWorktypeCode());
        model.setWorktypeName(dto.getWorktypeName());
        model.setFailureSolveSituation(dto.getFailureSolveSituation());
        model.setHelperEngineerCode(dto.getHelperEngineerCode());
        model.setHelperEngineerName(dto.getHelperEngineerName());

        final List<Map<String, Object>> assets = dto.getAssets();
        if (null != assets) {
            final String assetCodes = assets.stream()
                    .map(item -> item.get("code"))
                    .filter(Objects::nonNull)
                    .map(item -> item.toString())
                    .collect(Collectors.joining(","));
            model.setAssetCodes(assetCodes);
            model.setAssetCodes(assetCodes);
        }

        model.setStatus(dto.getStatus());
        return model;
    }

    @Override
    public MalfunctionFrontlineProcessingFormDTO toDTO(final MalfunctionFrontlineProcessingForm model,
            final boolean forListView) {

        final MalfunctionFrontlineProcessingFormDTO dto = new MalfunctionFrontlineProcessingFormDTO();
        dto.setId(model.getId());
        dto.setTaskId(model.getTaskId());
        dto.setWorkNum(model.getWorkNum());
        dto.setFailureLevelCode(model.getFailureLevelCode());
        dto.setFailureLevelName(model.getFailureLevelName());
        dto.setSolved(model.getSolved());
        dto.setWorktypeCode(model.getWorktypeCode());
        dto.setWorktypeName(model.getWorktypeName());
        dto.setFailureSolveSituation(model.getFailureSolveSituation());
        dto.setHelperEngineerCode(model.getHelperEngineerCode());
        dto.setHelperEngineerName(model.getHelperEngineerName());

        if (!StringUtils.isEmpty(model.getAssetCodes())) {
            final List<String> codes = Stream.of(model.getAssetCodes().split(",")).collect(Collectors.toList());
            if (null != codes) {
                final Map<String, String[]> assetCodes = new HashMap<>();
                assetCodes.put("assetCodes", codes.toArray(new String[codes.size()]));
                final ResultListDTO<Map<String, Object>> assets =
                        this.assetMicroService.AssetByCodes(assetCodes);
                if (assets.isSuccess()) {
                    dto.setAssets(assets.getData());
                }
            }
        }

        dto.setStatus(dto.getStatus());
        return dto;
    }

}
